
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
         
          $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function(){
            return "自定义placeholder文本";
          };

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'warehouse/inventorylist/index',
                    add_url: 'warehouse/inventorylist/add',
                    multi_url: 'warehouse/inventorylist/multi',
                    record_url: 'warehouse/inventorylist/record',
                    table: 'inventory_list',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'itemname', title: __('Itemame')},
                        {field: 'sku', title: __('Sku'),operate:'IN'},
                        {field: 'inventorycount', title: __('Inventorycount'),operate:false},
                        {field: 'last_in_time', title: __('Instoragetime'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'last_out_time', title: __('Outstoragetime'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'warehouse_code', title: __('warehouse_code'),operate:false,formatter:Controller.api.formatter.checkwarehouse},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    title: __('商品出入库记录'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'warehouse/inventorylist/record'
                                }
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ],
                search:false,
                showToggle: false,
                searchFormVisible:true,
                exportDataType: 'selected',
                exportTypes: ['doc', 'excel']
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on('click','.in-warehouse',function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                if(ids.length == 0){
                    Layer.confirm(
                        __('No in item', ids.length),
                        {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                        ''
                    );
                }else {
                    var url = "warehouse/inventorylist/inWarehouse";
                        ids = ($.isArray(ids) ? ids.join(",") : ids);
                        url = Table.api.replaceurl(url, {ids:ids}, table);
                        // $(that).data().width='880px';
                    Fast.api.open(url, '入库', $(that).data() || {});
                }
            });

            $(document).on('click','.out-warehouse',function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                if(ids.length == 0){
                    Layer.confirm(
                        __('No out item', ids.length),
                        {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                        ''
                    );
                }else {
                    var url = "warehouse/inventorylist/outWarehouse";
                    ids = ($.isArray(ids) ? ids.join(",") : ids);
                    url = Table.api.replaceurl(url, {ids:ids}, table);
                    Fast.api.open(url, '出库', $(that).data() || {});
                }
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        inwarehouse:function () {

            $(document).on('click','.del-item',function () {
                if($(".pd-item").length > 1){
                    $(this).parent().remove();
                }else {
                    $(this).remove();
                }
            });
            Controller.api.bindevent();
        },
        outwarehouse: function () {
            $(document).on('click','.del-item',function () {
                if($(".pd-item").length > 1){
                    $(this).parent().remove();
                }else {
                    $(this).remove();
                }
            });
            Controller.api.bindevent();
        },


        record1111:function(){
          var ids = $("#ids").val();
          $("#table").bootstrapTable({
            ajax: function (request) {
                $.ajax({
                    type: "GET",
                    url: 'warehouse/inventorylist/record?ids='+ids,
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    json: 'callback',
                    success: function (json) {
                        var columnsArray = [];

                        for (var i = 0; i < (Object.keys(json['columns'])).length; i++) {//Object.keys(obj) 获取key名称
                            var columnName = (Object.keys(json['columns']))[i];//id   username
                            var property = json['columns'][columnName];
                            if(columnName=='type'){
                              columnsArray.push({
                                "title": property['title'],
                                "field": property['field'],
                                operate:true,
                                formatter:Controller.api.formatter.checktype
                              });
                            }else if(columnName == 'status'){
                              columnsArray.push({
                                "title": property['title'],
                                "field": property['field'],
                                operate:true,
                                formatter: Controller.api.formatter.checkstatus
                              });
                            }else{
                              columnsArray.push({
                                "title": property['title'],
                                "field": property['field'],
                                operate:true
                              });
                            }
                           
                        }
                        
    
                        $('#table').bootstrapTable('destroy').bootstrapTable({
                            data: json['rows'],
                            // toolbar: '#toolbar',
                            // singleSelect: true,
                            // clickToSelect: true,
                            pk: 'id',
                            sortName: 'id',
                            sortOrder: "desc",
                            // pageSize: 15,
                            // pageNumber: 1,
                            // pageList: "[10, 25, 50, 100, All]",
                            // search: true,
                            // pagination: true,
                            columns: columnsArray,
                            search:false,
                            showToggle: false,
                            searchFormVisible:true,
                            showExport: false,
                            showColumns: true,
                            pageSize: 10,
                            onLoadSuccess: function (e) {
                              debugger;
                              alert('成功');
                            },
                            onLoadError: function (e) {
                              showTips("数据加载失败！");
                            },
                        });
                    },
                    error: function () {
                        alert("错误");
                    }
                });
            }
        });
        },

        record:function (e) {
            var ids = $("#ids").val();
            Table.api.init({
                extend: {
                    record_index_url: 'warehouse/inventorylist/record?ids='+ids
                }
            });
            var table = $("#table");
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.record_index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                      {field: 'create_time', title: __('日期'),addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                      {field: 'instoragecount', title: __('数量'),operate:false},
                      {field: 'orderid', title: __('平台订单号'),operate:false},
                      {field: 'count', title: __('实际出(入)库数量'),operate:false},
                      // {field: 'count', title: __('未入库'),operate:false},
                      {field: 'type', title: __('类型'),searchList:{1: "入库", 2: "出库"},formatter:Controller.api.formatter.checktype},
                      {field: 'warehouse_code', title: __('仓库'),searchList:{1: "法国Macway-斯特拉斯堡仓库"}},
                      {field: 'orderId', title: __('（出/入）库单号'), operate:false},
                      {field: 'status', title: __('状态'),operate:false,formatter: Controller.api.formatter.checkstatus}
                    ]
                ],
                
                search:false,
                showToggle: false,
                searchFormVisible:true,
                showExport: false,
                showColumns: true,
                pageSize: 10,
                onLoadSuccess: function (e) {
                  // alert('成功');
                },
                onLoadError: function (e) {
                  // showTips("数据加载失败！");
                },
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                checkwarehouse: function (value, row, index,wms) {
                    return '法国Macway-斯特拉斯堡仓库';
                },
                checkstatus: function (value, row, index) {
                  if(value == 2000){
                      return '<span class="status_audit">已审核</span>';
                  }else if(value == 2001){
                      return '<span class="status_reject">已拒绝</span>';
                  }else if(value == 3000){
                      return '<span class="status_checked">已确认</span>';
                  }else{
                      return '<span class="status_checking">审核中</span>';
                  }

              },
                checktype: function (value, row, index) {
                    if(value === 1){
                        return '入库';
                    }else if(value === 2){
                        return '出库';
                    }

                },
            }
        }
    };
    return Controller;
});